/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class BusinessBillItemDaoImpl extends BaseDaoImpl
		implements
			BusinessBillItemDao {
	public List<?> getList(long billId) {
		String sql = "from com.minierp.model.BusinessBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List<?> getList(long billId, String isCoop, String completeFlag) {
		String sql = "select i,ie from  com.minierp.model.BusinessBillItem i,com.minierp.model.BusinessBillItemExt ie where i.id=ie.id and i.billId="
				+ billId + " and i.isCoop=:isCoop";
		if (isCoop.equals("N")) {
			if (completeFlag != null && !completeFlag.equals("")) {
				sql = sql + " and ie.wcComp=:completeFlag";
			}
		} else if (completeFlag != null && !completeFlag.equals("")) {
			sql = sql + " and ie.coopComp=:completeFlag";
		}

		sql = sql + " order by i.id";
		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setString("isCoop", isCoop);
			if (completeFlag != null && !completeFlag.equals("")) {
				q.setString("completeFlag", completeFlag);
			}

			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public List<?> getList(long billId, long wcId, String completeFlag) {
		String sql = "select i,ie from  com.minierp.model.BusinessBillItem i,com.minierp.model.BusinessBillItemExt ie where i.id=ie.id and i.billId="
				+ billId + " and i.wcId=" + wcId;
		if (completeFlag != null && !completeFlag.equals("")) {
			sql = sql + " and ie.wcComp=:completeFlag";
		}

		sql = sql + " order by i.id";
		Session sess = null;

		List arg9;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (completeFlag != null && !completeFlag.equals("")) {
				q.setString("completeFlag", completeFlag);
			}

			arg9 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg9;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.BusinessBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteExt(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.BusinessBillItemExt where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public boolean isAllCompleted(long billId) {
		String sql = "select count(i.id) from com.minierp.model.BusinessBillItem i,com.minierp.model.BusinessBillItemExt ie where i.id=ie.id and i.billId="
				+ billId
				+ " and (i.wcId!=0 and ie.wcComp=\'N\' or i.isCoop=\'Y\' and ie.coopComp=\'N\')";
		List list = this.getHibernateTemplate().find(sql);
		int count = 0;
		if (!list.isEmpty() && list.get(0) != null) {
			count = Integer.parseInt(list.get(0).toString());
		}

		return count <= 0;
	}

	public List getStatus(long billId) {
		String sql = "select ie,i.wcId from com.minierp.model.BusinessBillItem i,com.minierp.model.BusinessBillItemExt ie where i.id=ie.id and i.billId="
				+ billId;
		return this.getHibernateTemplate().find(sql);
	}

	public List getListWidthExt(long billId) {
		String sql = "select i,ie from com.minierp.model.BusinessBillItem i,com.minierp.model.BusinessBillItemExt ie where i.id=ie.id and i.billId="
				+ billId;
		return this.getHibernateTemplate().find(sql);
	}

	public List getProductList(long billId) {
		String sql = "select p,i.priceType,i.amount,i.squareNum from com.minierp.model.BusinessBillItem i,com.minierp.model.Product p where i.proId=p.id and i.billId="
				+ billId + " order by i.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List getProductList(long billId, long wcId) {
		String sql = "select p,i.priceType,i.amount,i.squareNum from com.minierp.model.BusinessBillItem i,com.minierp.model.Product p where i.proId=p.id and i.billId="
				+ billId + " and i.wcId=" + wcId + " order by i.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int updatePrintNum(final long[] idArr, final String printerName) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						if (idArr != null && idArr.length != 0) {
							String str = "";

							for (int hql = 0; hql < idArr.length; ++hql) {
								if (!str.equals("")) {
									str = str + ",";
								}

								str = str + idArr[hql];
							}

							String arg3 = "update com.minierp.model.BusinessBillItemExt set printNum=printNum+1,lastPrinter=:lastPrinter,printTime=:printTime where id in ("
									+ str + ")";
							return new Integer(session.createQuery(arg3)
									.setString("lastPrinter", printerName)
									.setTimestamp("printTime", new Date())
									.executeUpdate());
						} else {
							return Integer.valueOf(0);
						}
					}
				});
		return ((Integer) obj).intValue();
	}
}